package cn.donting.jme.edit.ui.inspector.imp.light;

import cn.donting.jme.edit.attribute.*;
import cn.donting.jme.edit.ex.AttributeLoader;
import cn.donting.jme.edit.ex.InspectorUILoader;
import cn.donting.jme.edit.ui.inspector.AttributeInspectorUI;
import cn.donting.jme.edit.ui.inspector.InspectorUI;
import com.jme3.light.Light;
import com.jme3.light.LightList;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Spatial;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;


/**
 * LightAttribute 基本构建
 * 也是兜底
 */
public class LightAttributeLoader implements AttributeLoader<Light> {

    /**
     * 获取 light 基本Attribute
     *
     * @param light
     * @return
     */
    private List<Attribute> getLightBaseAttribute(Light light) {
        List<Attribute> attributes = new ArrayList<>();

        StringAttribute stringAttribute = new StringAttribute("名称");
        stringAttribute.setValue(light.getName());
        stringAttribute.addListener((oldValue, newValue) -> light.setName(newValue));
        attributes.add(stringAttribute);


        AtomicReference<ColorRGBA> colorRGBA = new AtomicReference<>(light.getColor());
        AtomicReference<Float> intensity = new AtomicReference<>((float) 1);


        ColorRGBAAttribute colorRGBAAttribute = new ColorRGBAAttribute("颜色");
        colorRGBAAttribute.setValue(light.getColor());
        colorRGBAAttribute.addListener((oldValue, newValue) -> {
            colorRGBA.set(newValue);
            light.setColor(newValue.mult(intensity.get()));
        });
        attributes.add(colorRGBAAttribute);

        SliderAttribute sliderAttribute = new SliderAttribute("强度");
        sliderAttribute.setMax(2f);
        sliderAttribute.setMin(0f);
        sliderAttribute.setValue(1f);
        sliderAttribute.addListener((oldValue, newValue) -> {
            intensity.set(newValue);
            light.setColor(colorRGBA.get().mult(newValue));
        });
        attributes.add(sliderAttribute);

        return attributes;
    }


    @Override
    public List<Attribute> getAttribute(Light light) {
        return getLightBaseAttribute(light);
    }

    @Override
    public boolean isLoad(Object object) {
        if (object instanceof Light) {
            return true;
        }
        return false;
    }


}
